/*
* @Author: Flora
* @Date:   2019-01-18 21:53:03
* @Last Modified by:   Flora
* @Last Modified time: 2019-01-20 10:44:00
*/
require('./index.css');
require('page/common/nav/index.js');
require('page/common/footer/index.css');
require('util/jq-shake.js');
var _mm = require('util/mm.js');
var _product = require('service/product-service.js');
var _properties = require('util/properties.js');

var page = {
	init : function(){
		var _this = this;
		var productId = _mm.getUrlParam('id');
		_this.dispData(productId);
		_this.bindEvent();
	},
	dispData : function(productId){
		var _this = this;
		_product.show(productId, function(resData){
			var product = resData;
			$('.n-edit #title').val(resData.title);
			var product = resData;
			$('.n-edit #title').val(product.title);
			$('.n-edit #summary').val(product.summary);
			$('.n-edit #online-pic-url').val(product.picUrl);
			$('.n-edit #detail').val(product.detail);
			$('.n-edit #price').val(product.price);
			$('.n-edit #stock').val(product.stock);
			_this.dispPic(product.picUrl);
		},function(errMsg){

		});
	},
	bindEvent : function(){
		var _this = this;
		// 选择本地图片或者网络图片
		$(".n-edit input[name='pic']").click(function(){
			var pic = $(".n-edit input[name='pic']:checked").val();
			if(pic == 'url'){
				// 图片URL输入框
				$('.n-edit #urlUpload').css('display','block');
				$('.n-edit #fileUpload').css('display','none');
			}else if(pic == 'file'){
				// 本地图片提交按钮
				$('.n-edit #fileUpload').css('display','block');
				$('.n-edit #urlUpload').css('display','none');
			}
		});
		// 提交本地图片
		$(".n-edit #localPicload").click(function(){
			var formData = new FormData();
			var file = $('.n-edit #fileUp')[0].files[0];
			formData.append('upload_file',file);
			_product.uploadPic(formData,function(resData){
				// 预览本地图片
				// _this.dispPic(_properties.imageHost + resData.uri);
				console.log(_properties.imageHost + resData.uri);
			},function(errMsg){
				alert('上传失败');
			});			
		});
		// 预览网络图片
		$('.n-edit #online-pic-url').blur(function(){
			var picUrl=$(".n-edit #online-pic-url").val();
			_this.dispPic(picUrl);
			console.log(picUrl);
		});
		// 提交表单
		$(".n-edit #public-sub").click(function(){
			var product = {
				id		: _mm.getUrlParam('id'),
				title 	: $.trim($(".n-edit #title").val()),
				summary : $.trim($(".n-edit #summary").val()),
				picUrl 	: '',
				detail 	: $.trim($(".n-edit #detail").val()),
				price 	: $.trim($(".n-edit #price").val()),
				stock 	: $.trim($(".n-edit #stock").val()),
				status	: 1
			};			
			// var pic = $(".n-edit input[name='pic']:checked").val();
			product.picUrl = $(".n-edit #imgpre")[0].src; 
			var isValidate = _this.validate(product);
			if(isValidate){
				_this.edit(product);
			}
		});
	},
	// 图片预览
	dispPic : function(picUrl){
		// TODO 图片预览
		$('.n-edit #imgpre').attr('src',picUrl);
	},
	validate : function(product){
		if(!_mm.validate(product.title,'require')){			
			$('.n-edit #title').shake(10,5,400);
			return false;
		}
		if(!_mm.validate(product.summary,'require')){			
			$('.n-edit #summary').shake(10,5,400);
			return false;
		}
		if(!_mm.validate(product.picUrl,'require')){			
			var pic = $(".n-edit input[name='pic']:checked").val();
			if( pic == 'url'){
				$('.n-edit #online-pic-url').shake(10,5,400);
				return false;
			}else if(pic == 'file'){
				$('.n-edit #fileUp').shake(10,5,400);
				return false;
			}
		}
		if(!_mm.validate(product.detail,'require')){			
			$('.n-edit #detail').shake(10,5,400);
			return false;
		}
		if(!_mm.validate(product.price,'price')){			
			$('.n-edit #price').shake(10,5,400);
			return false;
		}
		if(!_mm.validate(product.stock,'integer')){			
			$('.n-edit #stock').shake(10,5,400);
			return false;
		}
		return true;
	},
	edit : function(product){
		_product.edit(product, function(resData){
			window.location.href = './show.html?id='+product.id;
		},function(errMsg){
			alert('修改商品信息失败');
		});
	}
};
$(function(){
	page.init();
});


